package gestoremulte.pack;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Environment;

public class WriteReadElementoLista {

	private static String LocationFile = Environment.getExternalStorageDirectory()+File.separator+"GestioneMulte"+File.separator+"Files"+ File.separator+"Lista";
	WriteReadElementoLista(){
		
	}
	
	public void initialize(){
		ArrayList<ElementoLista> lista = new ArrayList<ElementoLista>();
		FileOutputStream fout;
		try {
			fout = new FileOutputStream(LocationFile);
			ObjectOutputStream oos = new ObjectOutputStream(fout);
		    oos.writeObject(lista);
		    oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public List<HashMap<String,String>> readFile(){
		
		List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
		ArrayList<ElementoLista> lista = null;
		
		File file=new File(LocationFile);

	   

	      FileInputStream fint;
		try {
			fint = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fint);
			lista=(ArrayList<ElementoLista>) ois.readObject();
			ois.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      

		for (int i=0; i<lista.size(); i++){
			HashMap<String, String> map = new HashMap<String, String>();
        	map.put("gender", lista.get(i).getType());
        	map.put("nverb", lista.get(i).getNumVerbale());
        	map.put("data_ora", lista.get(i).getDataOra());
        	fillMaps.add(map);
		}
	     return fillMaps;
	   
	}
	
	public void writeElemento(ElementoLista el){
		
		ArrayList<ElementoLista> lista = null;
		
		File file=new File(LocationFile);

	   

	      FileInputStream fint;
		try {
			fint = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fint);
			lista=(ArrayList<ElementoLista>) ois.readObject();
			ois.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		lista.add(el);
		
		FileOutputStream fout;
		try {
			fout = new FileOutputStream(LocationFile);
			ObjectOutputStream oos = new ObjectOutputStream(fout);
		    oos.writeObject(lista);
		    oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	}
	
	public void deleteElemento(ElementoLista el){
		
		ArrayList<ElementoLista> lista = null;
		
		File file=new File(LocationFile);

	   

	      FileInputStream fint;
		try {
			fint = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fint);
			lista=(ArrayList<ElementoLista>) ois.readObject();
			ois.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for (int i=0; i<lista.size(); i++){
			if (el.isequal(lista.get(i))){
				lista.remove(i);
				break;
			}
		}
		
		FileOutputStream fout;
		try {
			fout = new FileOutputStream(LocationFile);
			ObjectOutputStream oos = new ObjectOutputStream(fout);
		    oos.writeObject(lista);
		    oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
